using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace pong_xna
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //1 - Cria variaveis

        Texture2D campo;
        Texture2D bola;
        Texture2D paletaEsquerda;
        Texture2D paletaDireita;

        KeyboardState teclado;

        Vector2 posicaoYpaletaesquerda;
        Vector2 posicaoYpaletadireita;

        Vector2 posicaoBola;
        Vector2 velocidadeBola;
        Random geradorNumeroAleatorio = new Random();

        Rectangle hitBoxPaletaEsquerda;
        Rectangle hitBoxPaletaDireita;
        Rectangle hitBoxBola;

        int pontosPlayer2 = 0;
        int pontosPlayer1 = 0;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //criar conteudo:
            campo = Content.Load<Texture2D>("campo");
            paletaEsquerda = Content.Load<Texture2D>("palheta");
            paletaDireita = Content.Load<Texture2D>("palheta");
            bola = Content.Load<Texture2D>("bola");

            posicaoYpaletaesquerda = new Vector2(0,200);
            posicaoYpaletadireita = new Vector2(graphics.PreferredBackBufferWidth - paletaEsquerda.Width,100);

            posicaoBola = new Vector2((graphics.PreferredBackBufferWidth - bola.Width) / 2, (graphics.PreferredBackBufferHeight - bola.Height) / 2);
            velocidadeBola = new Vector2(geradorNumeroAleatorio.Next(-5, 6), geradorNumeroAleatorio.Next(-5, 6));

           

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            teclado = Keyboard.GetState();

            posicaoBola += velocidadeBola;



            if (teclado.IsKeyDown(Keys.W))
            {
                posicaoYpaletaesquerda.Y -= 5;
            }

            if (teclado.IsKeyDown(Keys.S))
            {
                posicaoYpaletaesquerda.Y += 5;
            }

            if (teclado.IsKeyDown(Keys.Up))
            {
                posicaoYpaletadireita.Y -= 5;
            }

            if (teclado.IsKeyDown(Keys.Down))
            {
                posicaoYpaletadireita.Y += 5;
            }

            if (posicaoYpaletaesquerda.Y < 0)
            {

                posicaoYpaletaesquerda.Y = 0;
            }

            if (posicaoYpaletadireita.Y < 0)
            {

                posicaoYpaletadireita.Y = 0;
            }

            if (posicaoYpaletaesquerda.Y > graphics.PreferredBackBufferHeight - paletaEsquerda.Height)
            {
                posicaoYpaletaesquerda.Y = graphics.PreferredBackBufferHeight - paletaEsquerda.Height;

            }

            if (posicaoYpaletadireita.Y > graphics.PreferredBackBufferHeight - paletaDireita.Height)
            {
                posicaoYpaletadireita.Y = graphics.PreferredBackBufferHeight - paletaDireita.Height;
            }

            if (posicaoBola.Y > graphics.PreferredBackBufferHeight - bola.Height)
            {
                velocidadeBola.Y *= -1;
            }

            if (posicaoBola.Y < 0)
            {
                velocidadeBola.Y *= -1;
            }

            if (posicaoBola.X > graphics.PreferredBackBufferWidth - bola.Width)
            {
                //velocidadeBola.X *= -1;
                posicaoBola.X = (graphics.PreferredBackBufferWidth - bola.Width) / 2;
                posicaoBola.Y = (graphics.PreferredBackBufferHeight - bola.Height) / 2;
                pontosPlayer1++;
                velocidadeBola = new Vector2(geradorNumeroAleatorio.Next(-5, 6), geradorNumeroAleatorio.Next(-5, 6));
            }

            if (posicaoBola.X < 0)
            {
                //velocidadeBola.X *= -1;
                posicaoBola.X = (graphics.PreferredBackBufferWidth - bola.Width) / 2;
                posicaoBola.Y = (graphics.PreferredBackBufferHeight - bola.Height) / 2;
                pontosPlayer2++;
                velocidadeBola = new Vector2(geradorNumeroAleatorio.Next(-5, 6), geradorNumeroAleatorio.Next(-5, 6));
            }

            hitBoxBola = new Rectangle((int)posicaoBola.X, (int)posicaoBola.Y, bola.Width, bola.Height);
            hitBoxPaletaDireita = new Rectangle((int)posicaoYpaletadireita.X, (int)posicaoYpaletadireita.Y, paletaDireita.Width, paletaDireita.Height);
            hitBoxPaletaEsquerda = new Rectangle((int)posicaoYpaletaesquerda.X, (int)posicaoYpaletaesquerda.Y, paletaEsquerda.Width, paletaDireita.Height);

            if (hitBoxBola.Intersects(hitBoxPaletaEsquerda))
            {
                posicaoBola.X = posicaoYpaletaesquerda.X + bola.Width;
                velocidadeBola.Y *= -1;
                velocidadeBola.X *= -1;
            }

            if (hitBoxBola.Intersects(hitBoxPaletaDireita))
            {
                posicaoBola.X = posicaoYpaletadireita.X - bola.Width;
                velocidadeBola.Y *= -1;
                velocidadeBola.X *= -1;
            }


            if (velocidadeBola.X == 0)
            {
                velocidadeBola.X += 1;
            }

            if (velocidadeBola.Y == 0)
            {
                velocidadeBola.Y += 1;
            }

            base.Update(gameTime);

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            //3- Desenhar

            spriteBatch.Begin();

            spriteBatch.Draw(campo, new Vector2(0, 0), Color.White);

            spriteBatch.Draw(paletaEsquerda, posicaoYpaletaesquerda, Color.White);

            spriteBatch.Draw(paletaDireita, posicaoYpaletadireita, Color.Blue);

            spriteBatch.Draw(bola, posicaoBola, Color.White);

            spriteBatch.End();


            base.Draw(gameTime);
        }
    }
}
